home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / keyboard.swg / 0064_Keyboard I-O.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-01-27  |  10.2 KB  |  229 lines

  1. UNIT KeyIO;                {  Key I/O Unit, Last Updated Dec 08/93                }
  2.                         {  Copyright (C) 1993 Greg Estabrooks                 }
  3.                         {  Requires TP 6.0+ to compile.                 }
  4. INTERFACE
  5. {***********************************************************************}
  6. CONST                   { Define some keys to make later detection easy.}
  7.         Enter  = #13;   Esc    = #27;   HomeKey   = #0#71;
  8.         EndKey = #0#79; PgUp   = #0#73; PgDn      = #0#81;
  9.         InsKey = #0#82; DelKey = #0#83; BackSpace = #8;
  10.         Space = #32;
  11.  
  12.         UpArrow = #0#72;  DnArrow = #0#80;  LfArrow = #0#75;
  13.         RtArrow = #0#77;
  14.  
  15.         Alt_A =        #0#30;        Alt_B =        #0#48;        Alt_C = #0#46;        Alt_D = #0#32;
  16.         Alt_E =        #0#18;        Alt_F = #0#33;        Alt_G = #0#34;        Alt_H = #0#35;
  17.         Alt_I = #0#23;  Alt_J = #0#36;  Alt_K = #0#37;        Alt_L = #0#38;
  18.         Alt_M = #0#50;        Alt_N = #0#49;        Alt_O = #0#24;        Alt_P = #0#25;
  19.         Alt_Q =        #0#16;        Alt_R = #0#19;        Alt_S = #0#31;  Alt_T = #0#20;
  20.         Alt_U = #0#22;        Alt_V = #0#47;        Alt_W = #0#17;        Alt_X = #0#45;
  21.         Alt_Y = #0#21;  Alt_Z = #0#44;
  22.  
  23.         Alt_1 = #0#120; Alt_2 = #0#121; Alt_3 = #0#122; Alt_4 = #0#123;
  24.         Alt_5 = #0#124; Alt_6 = #0#125; Alt_7 = #0#126; Alt_8 = #0#127;
  25.         Alt_9 = #0#128; Alt_0 = #0#129;
  26.  
  27.         Alt_F1 =#0#104; Alt_F2 =#0#105; Alt_F3 =#0#106; Alt_F4 =#0#107;
  28.         Alt_F5 =#0#108; Alt_F6 =#0#109; Alt_F7 =#0#110; Alt_F8 =#0#111;
  29.         Alt_F9 =#0#112; Alt_F10=#0#113; Alt_F11=#0#139;
  30.         Alt_F12=#0#140;
  31.  
  32.         F1 = #0#59;  F2 = #0#60;  F3 = #0#61;  F4 = #0#62;  F5 = #0#63;
  33.         F6 = #0#64;  F7 = #0#65;  F8 = #0#66;  F9 = #0#67;  F10= #0#68;
  34.         F11= #0#133; F12= #0#134;
  35.  
  36.         Shift_F11 = #0#135; Shift_F12 = #0#136;
  37.               Ctrl_F11  = #0#137; Ctrl_F12  = #0#138;
  38. TYPE
  39.         Str02    = STRING[2];
  40. VAR
  41.         KeyHPtr  :WORD Absolute $40:$1A;    {  Ptr to front of key buf. }
  42.         KeyTPtr  :WORD Absolute $40:$1C;    {  Ptr to end of key buf.   }
  43.  
  44. FUNCTION CapsOn :BOOLEAN;
  45.                  {  Routine to determine if Caps Lock is on.            }
  46. FUNCTION NumLockOn :BOOLEAN;
  47.                  {  Routine to determine if Num Lock is on.             }
  48. FUNCTION InsertOn :BOOLEAN;
  49.                  {  Routine to determine if Insert is on.               }
  50. FUNCTION AltPressed :BOOLEAN;
  51.                  {  Routine to determine if ALT key is being held down. }
  52. FUNCTION CtrlPressed :BOOLEAN;
  53.                  {  Routine to determine if Ctrl key is being held down.}
  54. FUNCTION LeftShift :BOOLEAN;
  55.                  {  Routine to determine if L_Shift is being held down. }
  56. FUNCTION RightShift :BOOLEAN;
  57.                  {  Routine to determine if R_Shift is being held down. }
  58. FUNCTION ScrollOn :BOOLEAN;
  59.                  {  Routine to determine if Scroll Lock is on.          }
  60. PROCEDURE NumLock( Enable :BOOLEAN );
  61.                  {  Routine to Set/Unset the Numlock key.               }
  62. PROCEDURE CapsLock( Enable :BOOLEAN );
  63.                  {  Routine to Set/Unset the Capslock key.              }
  64. PROCEDURE ScrollLock( Enable :BOOLEAN );
  65.                  {  Routine to Set/Unset the Scrolllock key.            }
  66. FUNCTION EnhancedKeyBoard :BOOLEAN;
  67.                  { Test Bit 4 of byte at 0040:0096 to check for enhanced}
  68.                  { Keyboard.                                                }
  69. FUNCTION EKeyPressed :BOOLEAN;
  70.                  { Function to determine if a key was pressed. The      }
  71.                  { routine in the CRT Unit to do this didn't detect F11 }
  72.                  { or F12 keys.                                         }
  73. FUNCTION GetKey :STR02;
  74.                  {  Routine to Read Key Presses. Including F11 and F12. }
  75. PROCEDURE ClearKeyBuffer;
  76.                  { Routine to Clear Keyboard buffer.                    }
  77. PROCEDURE PauseKey;
  78.                  { Routine to wait for a keypress and then continue.    }
  79.  
  80. IMPLEMENTATION
  81. {***********************************************************************}
  82. FUNCTION CapsOn :BOOLEAN;
  83.                 {  Routine to determine if Caps Lock is on.             }
  84. BEGIN                           { Test the keyboard status bit for Caps.}
  85.   CapsOn := (Mem[$0040:$0017] AND $40) <> 0;
  86. END;{CapsOn}
  87.  
  88. FUNCTION NumLockOn :BOOLEAN;
  89.                  {  Routine to determine if Num Lock is on.             }
  90. BEGIN                           { Test keyboard status bit for NumLock. }
  91.   NumLockon := (Mem[$0040:$0017] AND $20) <>0
  92. END;{NumLockOn}
  93.  
  94. FUNCTION InsertOn :BOOLEAN;
  95.                  {  Routine to determine if Insert is on.               }
  96. BEGIN                           { Test keyboard status bit for insert.  }
  97.   InsertOn := (Mem[$0040:$0017] AND $80) <> 0;
  98. END;{InsertOn}
  99.  
  100. FUNCTION AltPressed :BOOLEAN;
  101.                  {  Routine to determine if ALT key is being held down. }
  102. BEGIN                           { Test keyboard status bit for ALT.     }
  103.   AltPressed := (Mem[$0040:$0017] AND $08) <> 0;
  104. END;{AltPressed}
  105.  
  106. FUNCTION CtrlPressed :BOOLEAN;
  107.                  {  Routine to determine if Ctrl key is being held down.}
  108. BEGIN                           { Test keyboard status bit for Ctrl.    }
  109.   CtrlPressed := (Mem[$0040:$0017] AND $04) <> 0;
  110. END;{CtrlPressed}
  111.  
  112. FUNCTION LeftShift :BOOLEAN;
  113.                  {  Routine to determine if L_Shift is being held down. }
  114. BEGIN                           { Test keyboard status bit for L_Shift. }
  115.   LeftShift := (Mem[$0040:$0017] AND $02) <>  0;
  116. END;{LeftShift}
  117.  
  118. FUNCTION RightShift :BOOLEAN;
  119.                  {  Routine to determine if R_Shift is being held down. }
  120. BEGIN                           { Test keyboard status bit for R_Shift. }
  121.   RightShift := (Mem[$0040:$0017] AND $1) <> 0;
  122. END;{RightShift}
  123.  
  124. FUNCTION ScrollOn :BOOLEAN;
  125.                  {  Routine to determine if Scroll Lock is on.          }
  126. BEGIN                           { Test keyboard status bit for S_Lock.  }
  127.     ScrollOn := (Mem[$0040:$0017] AND $10) <> 0;
  128. END;{ScrollOn}
  129.  
  130. PROCEDURE NumLock( Enable :BOOLEAN );
  131.                  {  Routine to Set/Unset the Numlock key.               }
  132. BEGIN
  133.   IF Enable THEN                                {  Turn on Bit 5        }
  134.       Mem[$40:$17] := Mem[$40:$17] OR $20
  135.   ELSE                                                {  Turn it Off 5        }
  136.       Mem[$40:$17] := Mem[$40:$17] AND $DF;
  137. END;{NumLock}
  138.  
  139. PROCEDURE CapsLock( Enable :BOOLEAN );
  140.                  {  Routine to Set/Unset the Capslock key.              }
  141. BEGIN
  142.  IF Enable THEN                                        {  Turn on Bit          }
  143.      Mem[$40:$17] := Mem[$40:$17] OR $40
  144.  ELSE                                                {  Turn it Off          }
  145.      Mem[$40:$17] := Mem[$40:$17] AND $BF;
  146. END;{CapsLock}
  147.  
  148. PROCEDURE ScrollLock( Enable :BOOLEAN );
  149.                  {  Routine to Set/Unset the Scrolllock key.            }
  150. BEGIN
  151.  IF Enable THEN                                        {  Turn on Bit          }
  152.      Mem[$40:$17] := Mem[$40:$17] OR $10
  153.  ELSE                                                {  Turn it Off          }
  154.      Mem[$40:$17] := Mem[$40:$17] AND $EF;
  155. END;{ScrollLock}
  156.  
  157. FUNCTION EnhancedKeyBoard :BOOLEAN;
  158.                 {  Test Bit 4 of byte at 0040:0096 to check for enhanced}
  159.                 {  Keyboard.                                                }
  160. BEGIN
  161.   EnhancedKeyBoard := (Mem[$40:$96] AND $10) = $10;
  162. END;{EnhancedKeyBoard}
  163.  
  164. FUNCTION EKeyPressed :BOOLEAN; ASSEMBLER;
  165.                  { Function to determine if a key was pressed. The      }
  166.                  { routine in the CRT Unit to do this didn't detect F11 }
  167.                  { or F12 keys.                                         }
  168. ASM
  169.   Mov AH,$11                    { Function to check for Enhanced key.   }
  170.   Int $16                       { Call keyboard INT.                    }
  171.   Jz @NoKey                     { If ZF not set then nothing pressed.   }
  172.   Mov AL,1                      { Otherwise return TRUE.                }
  173.   Jmp @Exit                     { Now goto Exit.                        }
  174. @NoKey:
  175.   Mov AL,0                      { Return a FALSE result.                }
  176. @Exit:
  177. END;{EKeyPressed}
  178.  
  179. FUNCTION GetKey :STR02;
  180.                 {  Routine to Read Key Presses. Including F11 and F12         }
  181. VAR
  182.         TKey :STR02;            { Hold key press info.                  }
  183. BEGIN
  184.   ASM
  185.     Call EnhancedKeyBoard       { Test for an Enhanced keyboard.        }
  186.     Cmp AL,1                    { If AL=1 THEN Enhanced Keyboard= TRUE. }
  187.     Je @Enhanced                { If it was TRUE then Get Enhanced key. }
  188.     Mov AH,0                    { If not TRUE get normal key.           }
  189.     Jmp @ReadKeyb
  190. @Enhanced:
  191.     Mov AH,$10                  { Function to get key from enhanced board.}
  192. @ReadKeyb:
  193.     Int $16                     { Call Int keyboard INT.                }
  194.     Mov TKey[1].BYTE,AL         { Load both Ascii code and scan code    }
  195.     Mov TKey[2].BYTE,AH         { into TKey.                            }
  196.   END;
  197.   IF (TKey[1] = #224) AND EnhancedKeyBoard THEN
  198.     BEGIN
  199.       IF KeyHPtr = $1E THEN
  200.          TKey := #0 + CHR(Mem[$40:$3D])
  201.       ELSE
  202.          TKey := #0 + CHR(Mem[$40:KeyHPtr-1]);
  203.     END;
  204.   IF TKey[1] <> #0 THEN         { If it wasn't an extended key then     }
  205.     TKey := TKey[1];            { return only Ascii code.               }
  206.   GeTKey := TKey;               { Return proper key result to user.     }
  207. END;{GetKey}
  208.  
  209. PROCEDURE ClearKeyBuffer;
  210.                  { Routine to Clear Keyboard buffer.                    }
  211. VAR
  212.    DKey :Str02;                 { Hold unwanted keystrokes.             }
  213. BEGIN
  214.   WHILE EKeyPressed DO
  215.     DKey := GetKey;             { Read in unwanted key press.           }
  216. END;{ClearKeyBuffer}
  217.  
  218. PROCEDURE PauseKey;
  219.                  { Routine to wait for a keypress and then continue.    }
  220. VAR
  221.         Ch :STR02;              { Holds dummy key press.                }
  222. BEGIN
  223.   ClearKeyBuffer;               { Clear buffer of any previous keys.    }
  224.   Ch := GetKey;                 { Call for a key.                       }
  225. END;{PauseKey}
  226.  
  227. BEGIN
  228. END.
  229. {***********************************************************************}